package com.color.admin.system.controller;


import com.color.admin.common.core.base.BaseController;
import com.color.admin.common.core.constant.Constant;
import com.color.admin.common.core.page.PageRequest;
import com.color.admin.common.core.result.ResultData;
import com.color.admin.common.logger.annotation.Log;
import com.color.admin.system.entity.BaseEntity;
import com.color.admin.system.entity.SysDict;
import com.color.admin.system.entity.SysDictDetail;
import com.color.admin.system.entity.query.DictQueryCriteria;
import com.color.admin.system.servie.DictDetailService;
import com.color.admin.system.servie.DictService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

/**
 * @author HaiQiu
 * @date 2022/1/18
 */
@Tag(name = "dict-controller", description = "字典管理接口")
@RestController
@RequestMapping("/system/dict")
public class DictController extends BaseController {

    @Autowired
    private DictService dictService;

    @Autowired
    private DictDetailService dictDetailService;

    @Log("字典目录添加")
    @Operation(summary = "字典目录添加", description = "字典目录修改，并附带返回添加之后的数据")
    @PostMapping
    public ResultData save(@RequestBody @Validated({BaseEntity.add.class}) SysDict sysDict) {
        return decide(Constant.INSERT, dictService.saveRes(sysDict));
    }

    @Log("字典目录修改")
    @Operation(summary = "字典目录修改", description = "字典目录修改，并附带返回修改之后的数据")
    @PutMapping
    public ResultData update(@RequestBody @Validated({BaseEntity.update.class}) SysDict sysDict) {
        return decide(Constant.UPDATE, dictService.updateRes(sysDict));
    }

    @Log("字典目录删除")
    @Operation(summary = "字典目录删除", description = "字典目录批量删除")
    @DeleteMapping
    public ResultData delete(@RequestBody Set<String> dictIds) {
        dictService.delete(dictIds);
        return decide(Constant.DELETE);
    }

    @Log("字典目录查询")
    @Operation(summary = "字典目录查询", description = "根据ID字典目录查询")
    @GetMapping
    public ResultData get(@Parameter(description = "字典目录ID") @RequestParam String dictId) {
        return decide(dictService.get(dictId));
    }

    @Log("字典目录树形")
    @Operation(summary = "字典目录树形", description = "字典目录查询树形结构")
    @GetMapping("/tree")
    public ResultData tree() {
        return decide(dictService.tree());
    }

    /**************************字典数据**************************/

    @Log("字典添加")
    @Operation(summary = "字典添加")
    @PostMapping("/detail")
    public ResultData saveDetail(@RequestBody @Validated(BaseEntity.add.class) SysDictDetail dictDetail) {
        return decide(Constant.INSERT,dictDetailService.saveRes(dictDetail));
    }

    @Log("字典修改")
    @Operation(summary = "字典修改")
    @PutMapping("/detail")
    public ResultData updateDetail(@RequestBody @Validated(BaseEntity.update.class) SysDictDetail dictDetail) {
        return decide(Constant.UPDATE,dictDetailService.updateRes(dictDetail));
    }

    @Log("字典删除")
    @Operation(summary = "字典删除", description = "字典批量删除")
    @DeleteMapping("/detail")
    public ResultData updateDetail(@RequestBody Set<String> dictIds) {
        dictDetailService.delete(dictIds);
        return decide(Constant.DELETE);
    }

    @Log("字典查询")
    @Operation(summary = "字典查询", description = "根据ID字典查询")
    @GetMapping("/detail")
    public ResultData updateDetail(@Parameter(description = "字典ID") @RequestParam String dictDetailId) {
        return decide(dictDetailService.get(dictDetailId));
    }

    @Log("字典查询所有")
    @Operation(summary = "字典查询所有", description = "根据code码查询字典，不传值默认查询所有")
    @GetMapping("/detail/all")
    public ResultData getByCode(@Parameter(description = "字典code码") @RequestParam(required = false) String code) {
        return decide(dictDetailService.getByCode(code));
    }

    @Log("字典分页查询")
    @Operation(summary = "字典分页查询", description = "字典分页查询")
    @PostMapping("/detail/list")
    public ResultData listDetail(@RequestBody PageRequest<DictQueryCriteria> request) {
        return decide(dictDetailService.list(request));
    }
}
